1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include<iostream> #include<cstdio> #include<algorithm> #include<cctype> #include<cstring> #include<cmath> #include<utility> using namespace std; inline int read(){ int w=0,x=0;char c=getchar(); while(!isdigit(c))w|=c=='-',c=getchar(); while(isdigit(c))x=x*10+(c^48),c=getchar(); return w?-x:x; } namespace star { const int maxn=5e4+10,maxm=12,S=1050,mod=1e9+7; int n,m,a[maxn][maxm],D,f[S],pos,b[maxm],ans; char s[maxn]; pair<int,int> solve(){ int num=0;bool ok=true;char lst='>'; pair<int,int> ans(1,0),now,tmp; while(lst!=')' and ++pos) if(isdigit(s[pos])) num=s[pos]^48; else if(s[pos]=='(') now=solve(),ok=false; else ok and (now=make_pair((D>>num)&1,(D>>num)&1^1),0), ok=true,tmp=make_pair(0,0), lst!='>' and (tmp.second=(tmp.second+1ll*ans.second*now.second)%mod,tmp.first=(tmp.first+1ll*(ans.first+ans.second)*(now.first+now.second)-1ll*ans.second*now.second%mod+mod)%mod), lst!='<' and (tmp.first=(tmp.first+1ll*ans.first*now.first)%mod, tmp.second=(tmp.second+1ll*(ans.first+ans.second)*(now.first+now.second)-1ll*ans.first*now.first%mod+mod)%mod), ans=tmp,lst=s[pos],num=0; return ans; } inline bool cmp(const int &x,const int &y){return a[pos][x]<a[pos][y];} inline void work(){ n=read(),m=read(); for(int i=0;i<m;i++) for(int j=0;j<n;j++) a[j][i]=read(); scanf("%s",s+1);s[strlen(s+1)+1]=')'; for(D=0;D<(1<<m);D++) pos=0,f[D]=solve().second; for(int i=0;i<n;i++){ for(int j=0;j<m;j++) b[j]=j; pos=i;sort(b,b+m,cmp); for(int j=0,d=0;j<m;d|=(1<<b[j++])) ans=(ans+1ll*(a[i][b[j]]-(j?a[i][b[j-1]]:0))*f[d])%mod; } printf("%d\n",ans); } } signed main(){ star::work(); return 0; }
|